# 注：“DCC计划” 为 吊（Diao）毛刷题词场（CiChang）简称
import pyautogui
import time
import sys
import random



# 用于测试
wrongPic = "wrong.png"

# 基础按钮
mumuIcon = "iconPic.jpg"
startPic = "gameStart.jpg"
firstStart = "firstStart.png"
nextIcon = "nextPic.jpg"
leftIcon = "leftKuoHao.jpg"
confirmIcon = "confirm.jpg"
againIcon = "again.jpg"
kuangkuang = "leftKuoHao.jpg"



# 基础全局变量
againFlag = 0  # 重来flag


# mouseClick(1,"left",img,reTry)
def mouseClick(clickTimes, lOrR, img, reTry):
    if reTry == 1:
        flag = 0
        while True:
            # 定位图片的位置 i
            location = pyautogui.locateCenterOnScreen(img, confidence=0.9)
            if location is not None:
                pyautogui.click(location.x, location.y, clicks=clickTimes, interval=0.2, duration=0.2, button=lOrR)
                break
            print("未找到匹配图片", img, ",1秒后重试")
            flag = flag + 1
            if flag > 10:
                sys.exit()
            time.sleep(1)
    elif reTry > 1:
        i = 1
        while i < reTry + 1:
            location = pyautogui.locateCenterOnScreen(img, confidence=0.9)
            if location is not None:
                pyautogui.click(location.x, location.y, clicks=clickTimes, interval=0.2, duration=0.2, button=lOrR)
                print("重复")
                i += 1
            time.sleep(1)


def jumpExplainMethod():
    global againFlag
    explain = pyautogui.locateCenterOnScreen(nextIcon, confidence=0.7)
    if explain is not None:
        print("explain坐标：", explain)
        pyautogui.click(explain, clicks=1, interval=0.2, duration=0.2, button="left")
        print("跳过解释完成")
        time.sleep(1)
        againFlag = 0
    else:
        againFlag = againFlag + 1
        print("againFlag", againFlag)


def jumpFillMethod():
    global againFlag
    fill = pyautogui.locateCenterOnScreen(confirmIcon, confidence=0.7)
    if fill is not None:
        print("fill坐标：", fill)
        pyautogui.click(fill, clicks=1, interval=0.2, duration=0.2, button="left")
        print("跳过填空完成")
        time.sleep(1)
        againFlag = 0
    else:
        againFlag = againFlag + 1
        print("againFlag", againFlag)


# 后期需要修改的主要方法
def answerMethod():
    global againFlag
    firstKuang = pyautogui.locateCenterOnScreen(kuangkuang, confidence=0.7)
    if firstKuang is not None:
        print("firstKuang坐标：", firstKuang)
        num = random.randint(1,2) #加入随机
        pyautogui.click(firstKuang.x,firstKuang.y+150*num, clicks=1, interval=0.2, duration=0.2, button="left")
        print("跳过填空完成")
        time.sleep(1)
        againFlag = 0
    else:
        againFlag = againFlag + 1
        print("againFlag", againFlag)


def againMethod():
    again = pyautogui.locateCenterOnScreen(againIcon, confidence=0.8)
    if again is not None:
        print("again坐标：", again)
        pyautogui.click(again, clicks=1, interval=0.2, duration=0.2, button="left")
        print("md再来一遍！！！")
        time.sleep(1)
        startGame()     # 重新开始
    else:
        print("qAq~~ 结束运行！！！")
        sys.exit()
        # 不是重新答题的时候，重新开始闯关
        # startGame()


def startGame():
    # 点击开始闯关按钮
    print("开始闯关")
    startButton = pyautogui.locateCenterOnScreen(startPic, confidence=0.8)              # 先尝试重新开始
    print("startButton", startButton)
    if startButton is not None:
        pyautogui.click(startButton, clicks=1, interval=0.2, duration=0.2, button="left")
    firstStartButton = pyautogui.locateCenterOnScreen(firstStart, confidence=0.8)       # 尝试正常开局
    print("firstStartButton", firstStartButton)
    if firstStartButton is not None:
        pyautogui.click(firstStartButton, clicks=1, interval=0.2, duration=0.2, button="left")
    print("开始闯关！！！")
    time.sleep(1)

    while(404):
        answerMethod()          # 答题
        jumpExplainMethod()     # 判断是否是解释
        jumpFillMethod()        # 判断是否是填空题
        if againFlag > 3:
            againMethod()


# 主线任务
def hzhWork():
    print("开始主线任务")
    # 打开mumu
    mumu = pyautogui.locateCenterOnScreen(mumuIcon, confidence=0.9)
    if mumu is not None:
        print("mumu坐标：", mumu)
        pyautogui.click(mumu, clicks=1, interval=0.2, duration=0.2, button="left")
        print("打开mumu完成")
        time.sleep(2)
    startGame()



if __name__ == '__main__':
    print("开始执行我爱沪江V1.01！")
    hzhWork()  # 主要方法
    # print("对局结束！")
    # location = pyautogui.locateCenterOnScreen(yes, confidence=0.9)
    # while True:
    #     pyautogui.click(clicks=1, interval=0.2, duration=0.2, button="left")
    #     time.sleep(0.3)

# print("正在确认当前坐标")
# location = pyautogui.locateCenterOnScreen(duihua, confidence=0.9)
# locationX = location.x
# locationY = location.y
# print(str(locationX) + " " + str(locationY))
# pyautogui.click(locationX + 240, locationY - 350, clicks=1, interval=0.2, duration=0.2, button="left")
